package com.genghua.app.utils;

import lombok.*;

import java.io.Serializable;

/**
 * Created by kaimin on 2019-07-09.
 * time : 10:07
 */
@Data
@Builder
@ToString
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private T data;

    public R() {
        this.code = 0;
        this.msg = "success";
    }

    public R(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    public R(T data, String msg) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
        this.msg = msg;
    }

    public R(Throwable e) {
        this.code = 0;
        this.msg = "success";
        this.msg = e.getMessage();
        this.code = 1;
    }

    public static <T> R.RBuilder<T> builder() {
        return new R.RBuilder();
    }

    public String toString() {
        return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public R(int code, String msg, T data) {
        this.code = 0;
        this.msg = "success";
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return this.code;
    }

    public R<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static class RBuilder<T> {
        private int code;
        private String msg;
        private T data;

        RBuilder() {
        }

        public R.RBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public R.RBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public R.RBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public R<T> build() {
            return new R(this.code, this.msg, this.data);
        }

        public String toString() {
            return "R.RBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }
}
